/**
 * 
 */
package com.eis.performacetest.threadpool;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;

import com.eis.performacetest.util.PropertyReader;

/**
 * @author sh-panwei01
 *
 */
public class ThreadPool {
	private static Logger logger = Logger.getLogger(ThreadPool.class);
	
	public void startTest() {
		ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
				PropertyReader.getTCorePoolSize(),
				PropertyReader.getTMaximumPoolSize(),
				PropertyReader.getTKeepAliveTime(), 
				TimeUnit.SECONDS,
				new ArrayBlockingQueue<Runnable>(1000),
				new ThreadPoolExecutor.CallerRunsPolicy());

		int taskCount = PropertyReader.getTConcurrentThreads();
		for (int i = 0; i < taskCount; i++) {
			try {
				String task = "task@ " + i;
				threadPool.execute(new TestThread(task));
			} catch (Exception e) {
				logger.error("Error when creating task, task ID: task@" + i, e);
			}
		}
	}
}
